package com.hht.baselibs.activity

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.hht.baselibs.viewModel.BaseViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel


/**
 * Author:chen'hu
 * Time:2023/7/31 11:37
 * Description:This is BaseLibActivity
 */
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
abstract class BaseLibActivity<VM : BaseViewModel, DB : ViewDataBinding>() : AppCompatActivity(),
    CoroutineScope by MainScope() {
    protected lateinit var viewDataBing: DB
    protected lateinit var mContext: Context
    protected lateinit var mViewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        mContext = this
        super.onCreate(savedInstanceState)
    }


    override fun setContentView(layoutResID: Int) {
        if (layoutResID < 1) {
            throw NullPointerException("layout is not null ...")
        }
        viewDataBing =
            DataBindingUtil.inflate(LayoutInflater.from(mContext), layoutResID, null, false)
        viewDataBing.lifecycleOwner = this
        super.setContentView(viewDataBing.root)

    }

    override fun onDestroy() {
        super.onDestroy()
        //取消顶级父协程
        cancel()
    }
}